package BadBoy;

import java.io.File;
import java.io.IOException;
import java.util.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import javax.xml.transform.dom.*;
import javax.xml.xpath.XPath;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.*;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

public class Xml {
	String dr = "E:\\Code\\Android\\BadBoy\\data\\badboy_script.xml";
	Element rootElement;
	/**
	 * @param suite
	 * @throws ParserConfigurationException
	 * @throws SAXException
	 * @throws IOException
	 * @throws TransformerException 
	 * @throws XPathExpressionException 
	 */
	public void insert_xml(String suite) throws ParserConfigurationException, SAXException, IOException, TransformerException, XPathExpressionException 
		{
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder documentBuilder = documentBuilderFactory
				.newDocumentBuilder();
		Document document = documentBuilder.parse(dr);
		Element _root = document.getDocumentElement();
		Element _Suite = document.createElement("Suite");
		rootElement.appendChild(_Suite);
		XPath xPath = XPathFactory.newInstance().newXPath();
		String expression = "/Root";
		Node node = (Node) xPath.compile(expression).evaluate(document,
				XPathConstants.NODE);
			NodeList _name = document.getElementsByTagName("Suite");
			Integer _Count = _name.getLength();
			Attr ID = document.createAttribute("id");
			ID.setValue(suite + _Count.toString());
			_Suite.setAttributeNode(ID);
		DOMSource source = new DOMSource(document);
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = transformerFactory.newTransformer();
		StreamResult result = new StreamResult(dr);
		transformer.transform(source, result);
	}

//	public void insert_xml(String suite, String test) 
//			throws SAXException, IOException,ParserConfigurationException, XPathExpressionException,TransformerException 
//		{
//		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
//				.newInstance();
//		DocumentBuilder documentBuilder = documentBuilderFactory
//				.newDocumentBuilder();
//		Document document = documentBuilder.parse(dr);
//		Element root = document.getDocumentElement();
//		XPath xPath = XPathFactory.newInstance().newXPath();
//		String expression = "/root/TestSuite[@id=" + suite
//				+ "]/Test[@id=" + test + "]/step[@id=" + step + "]";
//		Node node = (Node) xPath.compile(expression).evaluate(document,
//				XPathConstants.NODE);
//		if (null != node) {
//			Element url = document.createElement("url");
//			node.appendChild(url);
//			url.appendChild(document.createTextNode(value));
//			NodeList list = document.getElementsByTagName("url");
//			Integer idlength = list.getLength();
//			Attr att_url = document.createAttribute("id");
//			att_url.setValue(idlength.toString());
//			url.setAttributeNode(att_url);
//		}
//		DOMSource source = new DOMSource(document);
//		TransformerFactory transformerFactory = TransformerFactory
//				.newInstance();
//		Transformer transformer = transformerFactory.newTransformer();
//		StreamResult result = new StreamResult(dr);
//		transformer.transform(source, result);
//
//	}
//	public void insert_xml(String suite, String test, String step) 
//			throws SAXException, IOException,ParserConfigurationException, XPathExpressionException,TransformerException 
//		{
//		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
//				.newInstance();
//		DocumentBuilder documentBuilder = documentBuilderFactory
//				.newDocumentBuilder();
//		Document document = documentBuilder.parse(dr);
//		Element root = document.getDocumentElement();
//		XPath xPath = XPathFactory.newInstance().newXPath();
//		String expression = "/root/TestSuite[@id=" + suite
//				+ "]/Test[@id=" + test + "]/step[@id=" + step + "]";
//		Node node = (Node) xPath.compile(expression).evaluate(document,
//				XPathConstants.NODE);
//		if (null != node) {
//			Element url = document.createElement("url");
//			node.appendChild(url);
//			url.appendChild(document.createTextNode(value));
//			NodeList list = document.getElementsByTagName("url");
//			Integer idlength = list.getLength();
//			Attr att_url = document.createAttribute("id");
//			att_url.setValue(idlength.toString());
//			url.setAttributeNode(att_url);
//		}
//		DOMSource source = new DOMSource(document);
//		TransformerFactory transformerFactory = TransformerFactory
//				.newInstance();
//		Transformer transformer = transformerFactory.newTransformer();
//		StreamResult result = new StreamResult(dr);
//		transformer.transform(source, result);
//
//	}
	/**
	 * @throws ParserConfigurationException
	 * @throws TransformerException
	 */
	public Xml() 
	{
		try {
			DocumentBuilderFactory docFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
		    rootElement = doc.createElement("Root");
			doc.appendChild(rootElement);
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(
					new File(dr));
			transformer.transform(source, result);
			System.out.println("File saved!");
		} catch (ParserConfigurationException pce) {
			pce.printStackTrace();
		} catch (TransformerException tfe) {
			tfe.printStackTrace();
		}
	}
}
